feat: auto-detect ImageContext format for image-to-image generation#342
feat: auto-detect ImageContext format for image-to-image generation#342johnnygreco merged 6 commits intomainfrom
Conversation
Greptile SummaryEnables seamless chaining of image generation columns by auto-detecting Key improvements:
Testing:
Minor issue:
|
| Filename | Overview |
|---|---|
| packages/data-designer-config/src/data_designer/config/models.py | Adds auto-detection logic for ImageContext with _auto_resolve_context_value() and _format_base64_context() methods, makes data_type optional |
| packages/data-designer-config/src/data_designer/config/utils/image_helpers.py | Moves ImageFormat enum here from models.py, breaking circular import dependency |
| packages/data-designer-engine/src/data_designer/engine/column_generators/generators/base.py | Adds shared _build_multi_modal_context() method that passes base_path to context resolution for file path support |
| packages/data-designer-config/tests/config/test_models.py | Adds comprehensive tests for auto-detection: URL pass-through, base64 format detection, file path resolution with base_path |
| packages/data-designer-engine/tests/engine/column_generators/generators/test_image.py | Adds tests for shared context builder and auto-detection in image-to-image generation workflow |
| packages/data-designer-config/src/data_designer/config/init.py | Updates ImageFormat import path from models.py to utils.image_helpers |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
Start[ImageContext.get_contexts called with record] --> CheckDataType{data_type set?}
CheckDataType -->|Yes - Explicit mode| ExplicitURL{data_type == URL?}
CheckDataType -->|No - Auto-detect| AutoDetect[_auto_resolve_context_value]
ExplicitURL -->|Yes| ReturnURL[Return URL as-is]
ExplicitURL -->|No - BASE64| FormatBase64[Format with image_format]
AutoDetect --> CheckPath{base_path set AND is_image_path?}
CheckPath -->|Yes| TryLoad[load_image_path_to_base64]
CheckPath -->|No| CheckURL{is_image_url?}
TryLoad --> LoadSuccess{File loaded?}
LoadSuccess -->|Yes| DetectFormat[_format_base64_context]
LoadSuccess -->|No| CheckURL
CheckURL -->|Yes| ReturnURL
CheckURL -->|No| AssumeBase64[Assume base64 data]
AssumeBase64 --> DetectFormat
DetectFormat --> DecodeBytes[decode_base64_image]
DecodeBytes --> DetectImageFormat[detect_image_format from bytes]
DetectImageFormat --> BuildDataURI[Build data URI with detected format]
FormatBase64 --> BuildDataURI
ReturnURL --> End[Return context list]
BuildDataURI --> End
Last reviewed commit: 179252e
Additional Comments (1)
Consider relaxing this check to accept any Prompt To Fix With AIThis is a comment left during a code review.
Path: packages/data-designer-config/src/data_designer/config/utils/image_helpers.py
Line: 209
Comment:
`is_image_url()` requires URLs to contain image file extensions, but many valid image URLs don't have extensions (e.g., CDN URLs, presigned S3 URLs with query params, or URLs that return images dynamically). When auto-detection encounters such URLs, they'll fall through to base64 decoding and fail.
Consider relaxing this check to accept any `http://` or `https://` URL, or add a fallback that attempts to validate as a URL before trying base64 decode.
How can I resolve this? If you propose a fix, please make it concise. |
Closes #341
📋 Summary
Enables chaining image generation columns so that a generated image from one
ImageColumnConfigcan be passed asImageContextto a downstream column — without requiring users to specifydata_typeorimage_format. Previously, users had to manually setdata_type=ModalityDataType.BASE64andimage_format=ImageFormat.PNG, and it still broke in create mode because file paths couldn't be resolved.🔄 Changes
✨ Added
ImageContext.get_contexts(): whendata_typeis omitted, values are resolved as URLs (pass-through), file paths (loaded to base64 from disk), or raw base64 (format detected from magic bytes)_build_multi_modal_context()method onColumnGeneratorWithModelbase class, passingbase_pathso generated image file paths are resolved to base64 before being sent to model endpointsImageFormatenum moved toimage_helpers.pyas its canonical location (breaks circular import betweenmodels.pyandimage_helpers.py)sample_png_bytesandminimal_png_base64test fixtures in config package conftest🔧 Changed
ImageContext.data_typeis now optional (Noneby default) — existing explicit usage continues to work unchangedImageContext.get_contexts()accepts a newbase_pathkeyword argument for file path resolutionImageCellGeneratorandColumnGeneratorWithModelChatCompletionto use shared_build_multi_modal_context()📚 Docs
ImageColumnConfigcolumns (text → image → edited image), replacing the old HuggingFace dataset loading approachImageContextusage to use auto-detection instead of explicit parameters🔍 Attention Areas
models.py- Core auto-detection logic in_auto_resolve_context_value()and_format_base64_context()base.py- Shared_build_multi_modal_context()method on the generator base classimage_helpers.py-ImageFormatenum moved here frommodels.pycloses #341